<?php

class app_order_device_bulletin_widget
{
    /**
     *
     * 存入数据表
     */
    public function save_admin_app_order_device_bulletin_day($time){
        if (!$time)  $time = strtotime('-1 day');
        $start_time = date('Y-m-d', $time).' 00:00:00';
        $end_time = date('Y-m-d', $time).' 23:59:59';

        //存入合伙人,门店,设备绑定统计信息表
        //合伙人APP信息
        $partner_dealer_data_list = bulletin_helper::get_partner_app_info_list($time);
        //掌柜APP信息
        $manager_dealer_data_list = bulletin_helper::get_manager_app_data_list($time);
        //合伙人订单信息
        $partner_order_data_list = bulletin_helper::get_partner_order_list($time);
        //掌柜订单信息
        $manager_order_data_list = bulletin_helper::get_manager_order_data_list($time);
        //设备绑定信息
        $device_data_list = bulletin_helper::get_device_data_list($time);

        $data = array();
        $data['partner_dealer_data'] = json_encode($partner_dealer_data_list);
        $data['manager_dealer_data'] = json_encode($manager_dealer_data_list);
        $data['partner_order_data'] = json_encode($partner_order_data_list);
        $data['manager_order_data'] = json_encode($manager_order_data_list);
        $data['device_bind_data'] = json_encode($device_data_list);
        $data['date'] = $this->get_date('day', $time);
        $data['add_time'] = date('Y-m-d H:i:s');

        $info = _uri('admin_app_order_device_bulletin_day', array('date' => $data['date']));
        if ($info) {
            // 更新
            _model('admin_app_order_device_bulletin_day')->update($info['id'], $data);
        } else {
            // 添加
            _model('admin_app_order_device_bulletin_day')->create($data);
        }

    }

    /**
     * 按照时间返回对应的日，周，月，统计用
     * @param unknown_type $type
     * @param unknown_type $time
     * @return string
     */
    private function get_date($type, $time = '') {
        if (!$time) {
            $time = time();
        }

        if ($type == 'day') {
            $date = date('Y-m-d', $time);
        } else {
            if ($type == 'week') {
                // 第几周
                $date = strftime('%Y%W', $time);
            } else {
                if ($type == 'month') {
                    $date = date('Ym', $time);
                }
            }
        }
        return $date;
    }
}